<?php

namespace Biz\Resources\User;

use Biz\Dictionaries\TitleDict;
use Biz\Dictionaries\GenderDict;
use Biz\Dictionaries\CountryDict;
use Lumen\Http\Resources\Json\Resource;

/**
 * UserMemberResource.
 *
 * @license [http://www.85do.com] [杭州永奥网络科技有限公司]
 * @copyright Copyright (c) 2018-2026 Hangzhou Yongao Technology Co., Ltd. All rights reserved.
 */
class UserMemberResource extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function toArray($request)
    {
        return [
            'uid'                => $this->uid,
            'title'              => $this->title,
            'title_name'         => $this->title ? TitleDict::get($this->title) : $this->title,
            'gender'             => $this->gender,
            'gender_name'        => $this->gender ? GenderDict::get($this->gender) : $this->gender,
            'first_name'         => $this->first_name,
            'middle_name'        => $this->middle_name,
            'last_name'          => $this->last_name,
            'country'            => $this->country,
            'country_name'       => $this->country ? CountryDict::get($this->country) : $this->country,
            'address'            => $this->address,
            'affiliation'        => $this->affiliation,
            'department'         => $this->department,
            'institution'        => $this->institution,
            'fax_number'         => $this->fax_number,
            'phone_number'       => $this->phone_number,
            'mobile_number'      => $this->mobile_number,
            'created_at'         => $this->created_at->getTimestamp(),
            $this->mergeWhen($this->relationLoaded('user'), [
                'user' => $this->getUserResource(),
            ]),
        ];
    }

    /**
     * Get resource.
     *
     * @return UserResource|null
     */
    public function getUserResource()
    {
        if ($this->relationLoaded('user')) {
            return new UserResource($this->user);
        }

        return null;
    }
}
